﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Microserve.TeleEye.Common.Products;
using Microserve.TeleEye.Data;
using System.Xml;
using Microserve.TeleEye.Common.Utility;

namespace Microserve.TeleEye.BusinessServices
{
    public class FeaturedProductManager
    {
        public List<Product> GetProductList(HttpContext context, string productGroup)
        {
            List<Product> featuredProducts = new List<Product>();

            string path = PathHelper.GetSectionPath(context, productGroup);

            ProductListManager plManager = new ProductListManager();
            XmlNode productList = plManager.GetProductList(path);

            XmlNodeList list = productList.SelectNodes("product");

            foreach (XmlNode item in list)
            {
                Product p = new Product()
                {
                    Image = item.Attributes["imageurl"].Value,
                    Name = item.Attributes["name"].Value,
                    Url = item.Attributes["url"].Value,
                    Target = item.Attributes["target"].Value,
                    Description = item.Attributes["abstract"].Value,
                    IsNew = bool.Parse(item.Attributes["isnew"].Value)
                };

                featuredProducts.Add(p);
            }

            return featuredProducts;
        }

        public List<Product> GetFeaturedProducts(HttpContext context, string sectionName)
        {
            List<Product> featuredProducts = new List<Product>();

            string path = PathHelper.GetSectionPath(context, sectionName);

            SectionHome sectionHome = new SectionHome();
            XmlDocument featuredProductsDoc = sectionHome.GetFeaturedProduct(path);

            XmlNodeList list = featuredProductsDoc.SelectNodes("section/featuredproducts/product");

            foreach (XmlNode item in list)
            {
                Product p = new Product()
                {
                    Image = item.Attributes["imageurl"].Value,
                    Name = item.Attributes["title"].Value,
                    Url = item.Attributes["url"].Value,
                    Target = item.Attributes["target"].Value
                };

                featuredProducts.Add(p);
            }

            return featuredProducts;
        }
    }
}
